<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>会员充值</title>
    <meta content="width=device-width,initial-scale=1.0,maximum-scale=1.0,user-scalable=0" name="viewport"/>
    <meta content="yes" name="apple-mobile-web-app-capable"/>
    <meta content="black" name="apple-mobile-web-app-status-bar-style"/>
    <meta content="telephone=no" name="format-detection"/>
    <script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
<div>
    <a href="{:url('User/user_credit_log')}">我的账单</a>
</div>
<form action="{:url('User/recharge')}" method="post" id="form">
    <div>
        {foreach name="rule" item="v"}
            <input type="radio" name="recharge_type" value="{$v.code}" onclick="remove_input();">充值{$v.score}元<br/>
        {/foreach}
    </div>
    金额：<input type="text" name="amount" value="" placeholder="请输入金额" onfocus="remove_radio();"><br/>
    {if condition="$is_weixin eq 0"}
    <input type="radio" name="pay_type" value="1" >支付宝
    {/if}
    <input type="radio" name="pay_type" value="2">微信
    <input type="text" name="uid" value="{$member.uid}"><br/>
    <input type="button" value="充值" onclick="return check_form();">
</form>
</body>
</html>
<script>
    //清空input框内容
    function remove_input(){
        $('input[name=amount]').val('');
    }
    //清空radio选择
    function remove_radio(){
        $('input[name=recharge_type]').attr("checked",false);
    }

    /**
     * 充值
     */
    function check_form(){
        var reg = /^\d+(\.\d{1,2})?$/;
        var recharge_type = $('input[name=recharge_type]:checked').val();
        var pay_type = $('input[name=pay_type]:checked').val();
        var amount = $('input[name=amount]').val();
        var uid = $('input[name=uid]').val();
        var is_weixin = "{$is_weixin}";

        if(recharge_type == undefined && amount == ''){
            alert('请选择充值方式或者输入充值金额');
            return false;
        }
        if(amount !='' && amount < 0){
            alert('输入充值金额错误，请重新输入');
            return false;
        }
        if(amount!=''){
            if(!reg.test(amount)){
                alert('充值金额最多两位小数');
                return false;
            }
        }
        if(pay_type == undefined){
            alert('请选择充值类型');
            return false;
        }
//        $('#form').submit();        //表单提交
//        window.location.href = "{:url('Weixinpay/recharge')}?order_sn="+order_sn+"&pay_type="+pay_type+"&amount="+amount+"&uid="+uid;
        $.post("{:url('User/recharge')}",
            {pay_type:pay_type,amount:amount,uid:uid,recharge_type:recharge_type},
            function(data){
                if(data.code == 1){
                    if(pay_type == 1){      //支付宝支付
                        if(is_weixin == 0){
                            window.location.href = "{:url('Alipay/recharge')}?id="+data.data;
                        }
                    }else{
                        if(is_weixin == 1){     //微信端
                            window.location.href = "{:url('Weixinpay/recharge')}?id="+data.data;
                        }else{
                            window.location.href = "{:url('Weixinpay/recharge_code')}?id="+data.data;
                        }
                    }
                }else{
                    alert(data.msg);
                }
            }

        );
    }
</script>